package com.gc.server.controller.api;

import com.gc.server.entity.SchoolEntity;
import com.gc.server.entity.SchoolEntity;
import com.gc.server.entity.UserEntity;
import com.gc.server.service.SchoolService;
import com.gc.server.service.UserSchoolService;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/school")
public class ApiSchoolController {

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private UserSchoolService userSchoolService;

    /**
     * 某人学校列表
     * @param userId
     * @return
     */
    @GetMapping("/list/user")
    public R listByUser(Long userId){
        List<SchoolEntity> list = schoolService.findByUserId(userId);
        return R.ok(list);
    }

    /**
     * 某人学校列表
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginUserId Long userId,Integer filter){
        List<SchoolEntity> list = schoolService.findByUserId(userId);
        return R.ok(list);
    }

    /**
     * 某人路线学校列表
     * 去除已经分配路线的
     * @param userId
     * @return
     */
    @GetMapping("/route/list")
    public R routeList(@LoginUserId Long userId,Integer filter){
        List<SchoolEntity> list = schoolService.findByUserId(userId);
        return R.ok(list);
    }



    @GetMapping("/id")
    public R getSchool(@LoginUserId Long userId,Long id){
        SchoolEntity schoolEntity = schoolService.get(id);
        List<UserEntity> users = userSchoolService.findUserBySchoolId(id);
        //只需要我的下级
        users = users.stream().filter(user->user.getPid().equals(userId)).collect(Collectors.toList());
        return R.ok(schoolEntity).putExtra("user",users);
    }


    @PostMapping("/add")
    public R addSchool(@LoginUserId Long userId,@RequestBody SchoolEntity schoolEntity){
        schoolService.add(userId,schoolEntity);
        return R.ok();
    }


    /**
     * 某线路学校列表
     * @param routeId
     * @return
     */
    @GetMapping("/route")
    public R listByRote(Long routeId){
        List<SchoolEntity> list = schoolService.findByRouteId(routeId);
        return R.ok(list);
    }
}
